package com.groupseven.happycampus.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.groupseven.happycampus.common.bo.ParttimeDayBo;
import com.groupseven.happycampus.common.instener.WebParttimeDayListener;
import com.groupseven.happycampus.common.pojo.ParttimeDay;
import com.groupseven.happycampus.common.result.R;
import com.groupseven.happycampus.common.utils.OssUtils;
import com.groupseven.happycampus.common.vo.LayuiPageVo;
import com.groupseven.happycampus.service.ParttimeDayService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @CreateTime: 2022-07-26  20:35
 */
@RequestMapping("/parttimeday")
@RestController
public class ParttimeDayController {

    @Autowired
    private ParttimeDayService parttimeDayService;

    @RequestMapping("/parttimedaylist")
    public LayuiPageVo<ParttimeDay> parttimeDayPageData(@RequestParam Map<String, Object> params) {
        System.out.println(params);
        return parttimeDayService.parttimeDayPageData(params);
    }

    @RequestMapping("/deleteparttimeday")
    public R deleteParttimeDay(Integer id) {
        int result = parttimeDayService.deleteParttimeDay(id);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping("/deleteparttimedaybyids")
    public R deleteParttimeDayByIds(String ids) {
        int result = parttimeDayService.deleteParttimeDayByIds(ids);

        if (result > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping("/updateparttimeday")
    public R updateParttimeDay(ParttimeDay parttimeDay) {
        int result = parttimeDayService.updateParttimeDay(parttimeDay);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping("/insertparttimeday")
    public R insertParttimeDay(ParttimeDay parttimeDay) {
        int result = parttimeDayService.insertParttimeDay(parttimeDay);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @Autowired
    WebParttimeDayListener webParttimeDayListener;


    @RequestMapping("/upload")
    public R uploadPic(MultipartFile file) throws IOException {
        if (file != null) {

            //得到文件名字
            String originalFilename = file.getOriginalFilename();

            InputStream inputStream = file.getInputStream();

            String url = OssUtils.uploadFile(inputStream, originalFilename);


            // 把文件 url 写给前端
            return R.ok().data("url", url);

        }
        return R.error();
    }


    // 下载excel模板
    @RequestMapping("/templateDownload")
    public void templateDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 下载 excel 的响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");

        // 读取服务器上的模板文件，写道浏览器
        // 模板路径
        String realPath = request.getServletContext().getRealPath("/parttime/日兼职信息模板.xlsx");

        // 根据文件路径 创建 file对象
        File file = new File(realPath);
        // 把 file 对象转为字节数组
        byte[] bytes = FileUtils.readFileToByteArray(file);

        // 下载文件名 中文 进行转码
        String filename = URLEncoder.encode("日兼职信息模板", "utf-8");

        // 告诉浏览器下载文件
        response.setHeader("Content-Disposition", "attachment;filename=" + filename + ".xlsx");

        //得到指向浏览器的输出流  向浏览器写输出
        ServletOutputStream outputStream = response.getOutputStream();
        // 把字节数组 输出到浏览器
        outputStream.write(bytes);

        outputStream.close();


    }

    // 读取excel 文件数据 存到数据库中
    @RequestMapping("/import")
    public R importExcel(MultipartFile file) throws IOException {
        // 避免遇见请求报错
        if(file!=null){
            /**
             * 参数1  文件流
             * 参数2  读取到的数据封装到哪个数据类型中
             * 参数3  监听器  每读取一行数据 就会调用监听器中的 invoke  方法
             * */
            ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), ParttimeDayBo.class, webParttimeDayListener);

            // 得到 sheet 对象
            ExcelReaderSheetBuilder sheet = read.sheet();
            // 读取sheet
            sheet.doRead();

        }
        return R.ok();
    }


    //导出列表数据     把满足条件的数据 查询出来  写到excel 中  ，把excel 写到浏览器
    @RequestMapping("/export")
    public void export(ParttimeDay parttimeDay, HttpServletResponse response) throws IOException {
        //下载excel 的 响应数据格式
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 下载的文件名 是中文  要进行转码  要不然会出现乱码
        String filename = URLEncoder.encode("日兼职信息导出", "utf-8");

        // 告诉浏览器  下载文件
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        //得到指向浏览器的输出流  向浏览器写输出
        ServletOutputStream outputStream = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(outputStream, ParttimeDayBo.class);

        ExcelWriterSheetBuilder sheet = write.sheet();
        //准备满足条件的集合数据
        List<ParttimeDayBo> parttimeDayBos = parttimeDayService.parttimeDayExcelData(parttimeDay);
        // 把这个集合数据 写到 输出流
        sheet.doWrite(parttimeDayBos);
    }



}
